/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dbhandler;

import evthandler.EventMessage;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 *
 * @author Casper, Dennis, Jackie, Jakob
 * @version 1.00
 */
public class City {

    private String zip;
    private String city;

    /**
     *
     * @param zip
     */
    public City(String zip) {
        this.zip = zip;
        try {
            ResultSet rs = DbCon.getInstance().executeQuery("SELECT city FROM city WHERE zip='" + zip + "'");
            while (rs.next()) {
                city = rs.getString("city");
            }
        } catch (SQLException ex) {
            new EventMessage("SQL error in City", "There was an error performing the sql inquery\n").showException(ex);
        }
    }
    /**
     *
     */
    public City(){

    }

    /**
     *
     * @return
     */
    public ArrayList<City> getCities() {
        ArrayList<City> cities = new ArrayList<City>();
        ArrayList<String> zips = new ArrayList<String>();
        try {
            ResultSet rs = DbCon.getInstance().executeQuery("SELECT * from city ORDER BY zip");
            while(rs.next()){
               zips.add(rs.getString("zip"));
            }
            for(int i = 0; i < zips.size(); i++){
                cities.add(new City(zips.get(i)));
            }
        } catch (SQLException ex) {
            new EventMessage("SQL error in City.getCities()", "There was an error performing the sql inquery\n").showException(ex);
        }
        return cities;
    }

    /**
     *
     * @return
     */
    public String getZip() {
        return zip;
    }

    /**
     *
     * @param zip
     */
    public void setZip(String zip) {
        this.zip = zip;
    }

    /**
     *
     * @param city
     */
    public void setName(String city) {
        this.city = city;
    }

    /**
     *
     */
    public void insert() {
        try {
            DbCon.getInstance().executeUpdate("INSERT INTO city (zip, city) VALUES ('" + zip + "', '" + city + "')");
        } catch (SQLException ex) {
            new EventMessage("SQL error in City.insert()", "There was an error performing the sql inquery\n").showException(ex);
        }
    }
}
